package com.rui.study.micro.shoppingbag.controller;

import com.netflix.discovery.shared.Application;
import com.netflix.loadbalancer.ILoadBalancer;
import com.rui.study.commons.util.FileUtil;
import com.rui.study.micro.shoppingbag.client.AttachClient;
import com.rui.study.micro.shoppingbag.client.DiscoveryClient;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
@RestController
public class TestController {

    public static final int _1MB = 1024 * 1024;
    public static final String PATH = "d:\\temp\\download";

    @Autowired
    AttachClient attachClient;


    @GetMapping("download")
    ResponseEntity download(@RequestParam("filename") String filename) {
        Response response = attachClient.getInputStream(filename);
        if (response != null) {
            Response.Body body = response.body();
            InputStream in;
            try {
                in = body.asInputStream();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            FileUtil.save(in, PATH, filename);
            log.info("结果保存到" + PATH + File.separator + filename);
        }

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.ALL_VALUE);
        return new ResponseEntity(httpHeaders, HttpStatus.OK);
    }

    @GetMapping("hello")
    ResponseEntity sayHello() {
        return new ResponseEntity(attachClient.sayHello(), HttpStatus.OK);
    }

    @PostMapping("hello")
    ResponseEntity sayHello(@RequestParam("name") String name) {
        return new ResponseEntity(attachClient.sayHello(name), HttpStatus.OK);
    }

    @Autowired
    DiscoveryClient discoveryClient;

    /**
     * fetch application
     * @return
     */
    @GetMapping("fa")
    ResponseEntity fetchApplication(@RequestParam("appName") String appName) {
//        String body = attachClient.fetchApplication(appName);
        Application body = discoveryClient.fetchApplication(appName);
        log.info("body = {}", body);
        return new ResponseEntity(body, HttpStatus.OK);
    }


    @Autowired
    LoadBalancerClient loadBalancerClient;

    /**
     * fetch service
     * @return
     */
    @PostMapping("fs")
    ResponseEntity fetchService(@RequestBody String serviceId) {
        ServiceInstance serviceInstance = loadBalancerClient.choose(serviceId);
        log.info("serviceInstance = {}", serviceInstance);
        return new ResponseEntity(serviceInstance, HttpStatus.OK);
    }

}
